<template>
  <div class="container">
    <!------------- 添加项输入框 ----------->
    <header>
      <input
        type="text"
        autofocus
        v-model="addContent"
        @keydown.enter="add"
        placeholder="请输入待办事项"
      />
      <input type="checkbox" v-model="selectAll" />
    </header>
    <!-------------- 待办列表 -------------->
    <main>
      <ul>
        <li v-for="todo in filterdTodos" :key="todo.id">
          <div v-if="todo.id != current" class="view">
            <input
              type="checkbox"
              :checked="todo.completed"
              @change="changeState(todo, $event)"
            />
            <p @click="current = todo.id" :class="todo.completed ? 'done' : ''">
              {{ todo.content }}
            </p>
            <a @click="del(todo.id)">×</a>
          </div>
          <input
            v-else
            @vnode-mounted="({ el }) => el.focus()"
            type="text"
            v-model="todo.content"
            @blur="edit()"
            @keydown.enter="edit()"
            @keydown.esc="cancel()"
          />
        </li>
      </ul>
    </main>
    <!-------------- 状态栏 ---------------->
    <footer>
      <div class="total">剩余{{ filters.ing(todos).length }}项</div>
      <div class="info">
        <button
          :class="showTodos == 'all' ? 'currentState' : ''"
          @click="showTodos = 'all'"
        >
          全部
        </button>
        <button
          :class="showTodos == 'ing' ? 'currentState' : ''"
          @click="showTodos = 'ing'"
        >
          进行中
        </button>
        <button
          :class="showTodos == 'done' ? 'currentState' : ''"
          @click="showTodos = 'done'"
        >
          已完成
        </button>
      </div>
      <a
        @click="delAll"
        :style="{
          visibility: filters.done(todos).length ? 'visible' : 'hidden',
        }"
        >清空完成项</a
      >
    </footer>
  </div>
</template>

<script setup>
import { onMounted, ref, computed } from 'vue'

/* 响应式数据用ref, 非响应式的直接声明 */
// 过滤函数工具集
const filters = {
  all: (todos) => todos,
  ing: (todos) => todos.filter((todo) => !todo.completed),
  done: (todos) => todos.filter((todo) => !!todo.completed),
}
// 属性
const addContent = ref('')
const todos = ref([])
const current = ref(0)
const showTodos = ref('all')

// 挂载阶段
onMounted(() => {
  getData()
})

// 计算属性, 显示某种条件的待办项
const filterdTodos = computed(() => {
  return filters[showTodos.value](todos.value)
})

// 计算属性, 用set和get 实现全选
const selectAll = computed({
  get: () => {
    return filters.ing(todos.value).length == 0
  },
  set: (value) => {
    // 全选, 将未完成项改为已完成
    filters.all(todos.value).forEach((todo) => {
      todo.completed = value ? 1 : 0
    })
    update()
  },
})

// 数据持久化, 存入localStorage
function update() {
  localStorage.setItem('todos', JSON.stringify(todos.value))
}

// 修改待办状态
function changeState(todo, e) {
  // console.log(completed)
  todo.completed = e.target.checked
  update()
}

// 获取数据
function getData() {
  todos.value = JSON.parse(localStorage.getItem('todos'))
    ? JSON.parse(localStorage.getItem('todos'))
    : []
}

// 修改待办
function edit() {
  current ? update() : 0
  current.value = 0
}

// 添加待办项
function add() {
  let todo = { id: Date.now(), content: addContent.value, completed: 0 }
  todos.value.push(todo)
  update()
  addContent.value = ''
}

// 删除待办
function del(id) {
  /* 方案1:  过滤出不含此项的其他项
      方案2:  根据index splice
   */
  todos.value = todos.value.filter((todo) => todo.id != id)
  update()
}

// 清空
function delAll() {
  todos.value = filters.ing(todos.value)
  update()
}

// 取消修改
function cancel() {
  current.value = 0
  getData()
}
</script>

<style scoped>
/*----------- 通用样式 ----------*/
* {
  margin: 0;
  padding: 0;
}
a,
button,
input[type='checkbox'] {
  cursor: pointer;
}

/*----------- 容器 ------------*/
.container {
  width: 350px;
  box-sizing: border-box;
  box-shadow: 0 0 3px 3px #ddd;
}

/* -----------添加栏 ----------*/
header {
  position: relative;
  width: 100%;
}
header input[type='text'] {
  width: 100%;
  padding: 10px 10px 10px 28px;
  border: 1px solid transparent;
  border-bottom: 1px solid #eee;
  outline: 0;
  font-size: 17px;
  color: #444;
  box-sizing: border-box;
}
header input[type='text']:focus {
  box-shadow: 0 0 3px 3px #0175ff;
}
header input[type='checkbox'] {
  position: absolute;
  left: 6px;
  top: 13px;
  width: 15px;
  height: 15px;
}

/*----------- 待办项 -----------*/
main ul {
  max-height: 200px;
  overflow: auto;
}
main li {
  border-top: 1px solid transparent;
  border-bottom: 1px solid #eee;
  padding-right: 3px;
}
main li .view {
  display: flex;
  justify-content: space-between;
  padding: 4px 5px;
  box-sizing: border-box;
}
main input[type='checkbox'] {
  width: 15px;
}
main p {
  flex: 1;
  margin-left: 8px;
  word-break: break-all;
  color: #444;
  /* overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis; */
}
main p.done {
  color: #ccc;
  text-decoration: line-through;
}
main a {
  visibility: hidden;
  color: #666;
  font-size: 18px;
}
main li:hover a {
  visibility: visible;
}
/* 修改框 */
main input[type='text'] {
  width: calc(100% - 20px);
  border: 1px solid transparent;
  box-shadow: 0px 0px 2px 2px #0175ff;
  padding: 4px 7px;
  margin-left: 20px;
  outline: 0;
  font-size: 16px;
  box-sizing: border-box;
}

/*----------- 状态栏 -----------*/
footer {
  display: flex;
  justify-content: space-between;
  padding: 6px 8px;
  font-size: 14px;
  color: #333;
  user-select: none;
}
footer button {
  border: 1px solid transparent;
  padding: 0px 4px;
  margin-right: 5px;
  border-radius: 3px;
  background: #fff;
}
footer button:hover {
  border: 1px solid #0175ff;
}
/* 当前状态 */
footer .currentState {
  border: 1px solid transparent !important;
  box-shadow: 0 0 2px 2px #0175ff;
}
/*----------- 滚动条 -----------*/
ul::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 5px;
  height: 1px;
}
ul::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
  background: rgba(0, 117, 255, 0.8);
}
ul::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  background: #ededed;
}
</style>
